package com.example.myapplication

import android.R.attr.gravity
import android.os.Bundle
import android.os.PersistableBundle
import android.util.Log
import android.view.ViewGroup
import android.view.ViewTreeObserver
import android.widget.Button
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.ConstraintSet
import com.google.android.material.internal.ViewUtils.dpToPx

class MultilingualActivity: AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // 获取视图引用
        val textView = findViewById<TextView>(R.id.textView3)
        val button = findViewById<Button>(R.id.button2)

        val location = IntArray(2)
        // 监听 View 树的布局完成事件
        button.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
            override fun onGlobalLayout() {
                // 布局完成后，移除监听器（避免重复触发）
                button.viewTreeObserver.removeOnGlobalLayoutListener(this)

                // 此时再获取坐标，就是真实显示的位置
                button.getLocationOnScreen(location)
                val x = location[0]
                val y = location[1]
                Log.d("ButtonPosition", "按钮真实位置：x=$x, y=$y")
            }
        })

        // 设置点击事件
        button.setOnClickListener {
            textView.text = resources.getString(R.string.clicked)
        }



    }

//    override fun onCreate(savedInstanceState: Bundle?) {
//        super.onCreate(savedInstanceState)
//        val linearLayout = LinearLayout(this)
//        linearLayout.orientation = LinearLayout.VERTICAL
//
//        val textView = TextView(this)
//        textView.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
//            ViewGroup.LayoutParams.WRAP_CONTENT)
//        textView.textSize = 30F
//        textView.text = getString(R.string.textView3)
//        linearLayout.addView(textView)
//        val button = Button(this)
//        button.width = ViewGroup.LayoutParams.MATCH_PARENT
//        button.height = ViewGroup.LayoutParams.WRAP_CONTENT
//        button.text = getString(R.string.button2)
//        linearLayout.addView(button)
//        setContentView(linearLayout)
//                // 设置点击事件
//        button.setOnClickListener {
//            textView.text = resources.getString(R.string.clicked)
//        }
//
//    }


//    override fun onCreate(savedInstanceState: Bundle?) {
//        super.onCreate(savedInstanceState)
//
//        // 创建根布局
//        val constraintLayout = ConstraintLayout(this).apply {
//            layoutParams = ConstraintLayout.LayoutParams(
//                ConstraintLayout.LayoutParams.MATCH_PARENT,
//                ConstraintLayout.LayoutParams.MATCH_PARENT
//            )
//        }
//
//        // 创建图片视图
//        val imageView = ImageView(this).apply {
//            id = ImageView.generateViewId()
//            layoutParams = ConstraintLayout.LayoutParams(298.dpToPx(), 260.dpToPx())
//            setImageResource(R.drawable.flag)
//        }
//
//        // 创建文本视图
//        val textView = TextView(this).apply {
//            id = TextView.generateViewId()
//            layoutParams = ConstraintLayout.LayoutParams(
//                ConstraintLayout.LayoutParams.WRAP_CONTENT,
//                ConstraintLayout.LayoutParams.WRAP_CONTENT
//            )
//            text = getString(R.string.textView3)
//        }
//
//        // 创建按钮
//        val button = Button(this).apply {
//            id = Button.generateViewId()
//            layoutParams = ConstraintLayout.LayoutParams(
//                ConstraintLayout.LayoutParams.WRAP_CONTENT,
//                ConstraintLayout.LayoutParams.WRAP_CONTENT
//            )
//            text = getString(R.string.button)
//        }
//
//        // 添加所有视图到根布局
//        constraintLayout.addView(imageView)
//        constraintLayout.addView(textView)
//        constraintLayout.addView(button)
//
//        // 设置约束关系
//        val constraintSet = ConstraintSet().apply {
//            clone(constraintLayout)
//
//            // 图片约束：顶部对齐父布局，水平居中
//            connect(imageView.id, ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP, 16.dpToPx())
//            connect(imageView.id, ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START)
//            connect(imageView.id, ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END)
//
//            // 文本约束：顶部对齐图片底部，水平居中
//            connect(textView.id, ConstraintSet.TOP, imageView.id, ConstraintSet.BOTTOM, 16.dpToPx())
//            connect(textView.id, ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START)
//            connect(textView.id, ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END)
//
//            // 按钮约束：顶部对齐文本底部，水平居中
//            connect(button.id, ConstraintSet.TOP, textView.id, ConstraintSet.BOTTOM, 16.dpToPx())
//            connect(button.id, ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START)
//            connect(button.id, ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END)
//        }
//
//        // 应用约束
//        constraintSet.applyTo(constraintLayout)
//
//        // 按钮点击事件
//        button.setOnClickListener {
//            textView.text = getString(R.string.clicked)
//        }
//
//        // 设置内容视图
//        setContentView(constraintLayout)
//    }
//
//    // dp转px工具方法
//    private fun Int.dpToPx(): Int {
//        return (this * resources.displayMetrics.density).toInt()
//    }



}